/*
 * This file is part of quickReminder, a software for gamers
 *
 * Copyright (C) 2006-2008 Lucas Romero
 *
 * This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 3 of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along with this program; if not, see <http://www.gnu.org/licenses/>.
 */
using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;

namespace UserNotifier
{
    public class SWKeyboardBlink : WarnModule
    {
        [DllImport("user32.dll")]
        static extern IntPtr keybd_event(IntPtr KEY, IntPtr bScan, IntPtr dwFlags, IntPtr dwExtraInfo);
        [DllImport("user32.dll")]
        static extern short GetKeyState(IntPtr KEY);

        public override WarnModuleTypes Type
        {
            get { return WarnModuleTypes.SWKeyboardBlink; }
        }

        public override string Description
        {
            get
            {
                return Info.SWKbdDesc;
            }
        }

        private int m_Duration;

        public int Duration
        {
            get { return m_Duration; }
            set { m_Duration = value; }
        }
        private KeyboardLEDs m_LED = KeyboardLEDs.CapsLock;

        public KeyboardLEDs LED
        {
            get { return m_LED; }
            set
            {
                m_LED = value;
                m_Key = new IntPtr(m_KeyConstantsSW[(int)value]);
            }
        }
        private IntPtr m_Key;
        private int m_OriginalState;
        private Int32[] m_KeyConstantsSW = { 0x14, 0x90, 0x91 };
        private const UInt32 KEYUP = 0x2;

        public SWKeyboardBlink(int interval, KeyboardLEDs blinkled, int duration)
            : base(interval)
        {
            LED = blinkled;
            
            m_Duration = duration;
        }

        public SWKeyboardBlink()
            : base()
        {
            LED = KeyboardLEDs.CapsLock;
        }

        protected override bool InitDelegate()
        {
            m_OriginalState = GetKeyState(m_Key);
            return true;
        }

        protected override void Warn()
        {
            keybd_event(m_Key, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero);
            keybd_event(m_Key, IntPtr.Zero, new IntPtr(KEYUP), IntPtr.Zero);

            System.Threading.Thread.Sleep(m_Duration);

            keybd_event(m_Key, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero);
            keybd_event(m_Key, IntPtr.Zero, new IntPtr(KEYUP), IntPtr.Zero);
        }

        protected override void CleanupDelegate()
        {
            if ((m_OriginalState & 1) != (GetKeyState(m_Key) & 1))
            {
                keybd_event(m_Key, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero);
                keybd_event(m_Key, IntPtr.Zero, new IntPtr(KEYUP), IntPtr.Zero);
            }
        }
    }
}
